package com.kh.pajx.sop.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.IStockDevDao;
import com.kh.pajx.sop.dao.IStockDevInDao;
import com.kh.pajx.sop.dao.IStockDevInDetailDao;
import com.kh.pajx.sop.dao.IStockDevOutDao;
import com.kh.pajx.sop.dao.IStockDevOutDetailDao;
import com.kh.pajx.sop.dao.IStockDeviceDicDao;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.domain.StockDeviceDic;
import com.kh.pajx.sop.domain.stock.StockDev;
import com.kh.pajx.sop.domain.stock.StockDevIn;
import com.kh.pajx.sop.domain.stock.StockDevInDetail;
import com.kh.pajx.sop.domain.stock.StockDevOut;
import com.kh.pajx.sop.domain.stock.StockDevOutDetail;
import com.kh.pajx.sop.service.IStockDeviceDicService;
import com.kh.pajx.sop.util.page.QueryHelper;
@Service
@Transactional
public class StockDeviceDicServiceImpl extends BaseServiceImpl<StockDeviceDic> implements IStockDeviceDicService{
	
	@Resource
	private IStockDeviceDicDao dicDao;
	@Resource
	private IStockDevInDao inDao;
	@Resource
	private IStockDevInDetailDao detailDao;
	@Resource
	private IStockDevOutDao outDao;
	@Resource
	private IStockDevOutDetailDao outDetailDao;
	@Resource
	private IStockDevDao devDao;
	public void save(StockDeviceDic deviceDic){
		dicDao.save(deviceDic);
		
	}
	
	public void update(StockDeviceDic deviceDic){
		dicDao.update(deviceDic);
	}
	
	public void delete(String uuid){
		dicDao.delete(uuid);
	}
	
	public List<StockDeviceDic> findAll(){
		
		return dicDao.findAll();
	}
	
	@Override
	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelper queryHelper) {
		return dicDao.getPageBean(pageNum, pageSize, queryHelper);
	}

	public StockDeviceDic getById(String uuid){
		return dicDao.getById(uuid);
	}
	
	@Override
	public void addStockDevin(StockDevIn in, List<StockDevInDetail> detail) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");// 设置日期格式
		String nowTime = df.format(new Date());// 构造成当前时间
		in.setInDate(nowTime);
		inDao.save(in);
		Long inCode = in.getInBillcode();
		for(StockDevInDetail inDetail:detail){
			inDetail.setInBillcode(inCode);
			String devId=inDetail.getDevUuid();
			StockDeviceDic devDic=dicDao.getById(devId);
			String devName = devDic.getDevName();
			String devUnit = devDic.getDevUnit();
			String partVer = devDic.getDevVer();
			String describe = devDic.getDescribe();
			String devSupplierId=devDic.getDevSupplierId();
			inDetail.setDevName(devName);
			inDetail.setDevUnit(devUnit);
			inDetail.setDevVer(partVer);
			inDetail.setDescribe(describe);
			inDetail.setDevSupplierId(devSupplierId);
			detailDao.save(inDetail);
			
		}
	}
	public void addStockDevOut(StockDevOut out,List<StockDevOutDetail> outDetail){
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");// 设置日期格式
		String nowTime = df.format(new Date());// 构造成当前时间
		out.setOutDate(nowTime);// 出库申请时间
		outDao.save(out);
		Long outCode = out.getOutBillcode();
		for(StockDevOutDetail outd:outDetail){
			
			outd.setOutBillcode(outCode);
			String devId=outd.getDevUuid();
		/*	StockDeviceDic devDic=dicDao.getById(devId);*/
			StockDev dev=devDao.getById(devId);
			String devName = dev.getDevName();
			String devUnit = dev.getDevUnit();
			String describe = dev.getDescribe();
			String devSupplier=dev.getDevSupplierId();
			outd.setDevSupplierId(devSupplier);
			outd.setDevName(devName);
			outd.setDevUnit(devUnit);
			outd.setDescribe(describe);
			outd.setDevVer(dev.getDevVer());
			outDetailDao.save(outd);
			
		}
	}
}
